// test()
const fetch = require("node-fetch");
const fs = require('fs');
const { HttpsProxyAgent } = require('https-proxy-agent');

// const { DynamicPool } = require('node-worker-threads-pool');

// const dynamicPool = new DynamicPool(6);


var proxyUrl = "http://127.0.0.1:7890"

// var ip = "111.59.4.88";
// var port = "9002";
// var proxyUrl = `http://${ip}:${port}`;
var agent = new HttpsProxyAgent('http://58.220.95.32:10174');
// proxyUrl = "http://58.220.95.32:10174"

testProxy2();
// testSingleProxy(proxyUrl);
// testProxy1()



const numThreads = 4; // 指定线程数
const workerPool = [];
const tasksQueue = [];



// const { Worker, isMainThread, parentPort, workerData } = require('worker_threads');
// if (isMainThread) {
//   // 主线程代码
 

//   for (let i = 0; i < numThreads; i++) {
//     const worker = new Worker(__filename);
//     worker.on('message', (result) => {
//       console.log('worker date":' + new Date());
//       // console.log(`Received result: ${result.statis}`);
//       //检查tasksQueue 中是否有任务。如果有任务，则将第一个任务发送给worker进行处理
//       if (tasksQueue.length > 0) {
//         worker.postMessage(tasksQueue.shift());
//       } else {
//         // worker.terminate();
//         // process.exit();
//       }
//     });
//     workerPool.push(worker);
//   }
//   testProxy1()
  
// } else {
//   // 工作线程代码
//   parentPort.on('message', async task => {
//     // var s = new Date()
//     // console.log('parentPort date: ' + s);
//     await testSingleProxy(task.proxyUrl);
//     // const data = await response.text(); // 或者 response.json() 如果你期望返回JSON
//     // console.log('parentPort end date: ', new Date() - s);
//     parentPort.postMessage('200');
//   });
// }

async function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms))
}



async function dealfetch(url, proxyUrl) {
  await fetchWithTimeout(url, proxyUrl)
    .then(response => {
      // console.log("response", response.status);
      if (response.status == 200) {
        console.log("代理成功, proxyUrl", proxyUrl);
      }
    })
    .catch(error => {
      // console.error('发生错误:', error.message);
    });
}

async function fetchWithTimeout(url, proxyUrl, timeout = 3000) {
  const controller = new AbortController();
  const { signal } = controller;
  var agent = new HttpsProxyAgent(proxyUrl);
  var options = { method: 'GET', agent: agent }
  return new Promise(async (resolve, reject) => {
    const timer = setTimeout(() => {
      controller.abort();
      reject(new Error('Timeout'));
    }, timeout);

    try {
      const response = await fetch(url, { ...options, signal });
      clearTimeout(timer);
      if (!response.ok) {
        throw new Error(`HTTP error! status: ${response.status}`);
      } else {
        resolve(response);
      }
    } catch (error) {
      reject(error);
    }
  });

}

async function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms))
}

//测试单个地址
async function testSingleProxy(proxyUrl) {
  // const timeout = 3000;
  try {
    await dealfetch('https://www.baidu.com', proxyUrl)
    
    // const res = await dynamicPool.exec({
    //   task() {
    //     try { // 捕获异常
    //       dealfetch('https://www.baidu.com', proxyUrl)
    //     } catch (error) {
    //       console.log('error', error);
    //     }
    //   },
    //   timeout
    // });
  } catch (err) {
    if (isTimeoutError(err)) {
      // deal with timeout.
    } else {
      // deal with other errors.
    }
  }




}

//测试https://api.openproxylist.xyz/http.txt 的ip
async function testProxy1(proxyUrl) {
  try {
    var res = await fetch('https://api.openproxylist.xyz/http.txt').then(response => response.text());
    // var res = fs.readFileSync('ip.txt').toString()
    var line = res.split('\n');
    if(line.length < 2){
      line = res.split('\r\n');
    }
    let ipPorts = [];
    for (var i = 0; i < line.length; i++) {
      var theLine = line[i].split(':');
      var ip = theLine[0];
      var port = theLine[1];
      if (!(ip && port)) {
        continue;
      }
      ipPorts.push(theLine);
    }
    fetchBaidu(ipPorts);
  } catch (error) {
    console.error('error', error);
  }
}

//测试 http://www.kxdaili.com/dailiip/1/1.html 分页 http://www.kxdaili.com/dailiip/1/2.html的ip
async function testProxy2() {
  var htmlText = await getHtemlText('http://www.kxdaili.com/dailiip/1/3.html');
  // console.log("htmlText", htmlText);
  // 使用正则表达式匹配所有的IP:端口组合
  let ipPortRegex = /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})<\/td>\s*<td>(\d+)/g;

  // 执行匹配操作
  let matches;
  let ipPorts = [];

  while ((matches = ipPortRegex.exec(htmlText)) !== null) {
    // matches[1] 是IP地址，matches[2] 是端口
    ipPorts.push(matches[1] + ':' + matches[2]);
  }

  // 输出结果
  // console.log(ipPorts, ipPorts.length);
  // ipPorts.push("58.220.95.32:10174");
  fetchBaidu(ipPorts);
}

function fetchBaidu(ipPorts) {
  for (var i = 0; i < ipPorts.length; i++) {
    var ipPort = ipPorts[i];
    var proxyUrl = `http://${ipPort}`;
    // console.log("proxyUrl", proxyUrl);
    testSingleProxy(proxyUrl)

    // const task = { proxyUrl };
    // // 将任务发送给工作线程
    // const idleWorker = workerPool.find(worker => worker.threadId !== null);
    // if (idleWorker) {
    //   // 将任务发送给空闲的工作线程
    //   idleWorker.postMessage(task);
    // } else {
    //   // 如果没有空闲的工作线程，则将任务放入队列
    //   tasksQueue.push(task);
    // }

  }
}

async function getHtemlText(url) {
  var htmlText = await fetch(url, {
    method: 'GET',
    headers: {

    }
    // body: body
  }).then(res => {
    // console.log("res", res)
    return res.text()
  });
  // console.log("htmlText", htmlText);
  return htmlText;
}

async function test() {

  var htmlText = await getHtemlText('https://api.89ip.cn/tqdl.html?api=1&num=60&port=&address=&isp=');
  // console.log("htmlText", htmlText);
  var match = htmlText.match(/(\d+.*)<br>/g);
  let ipPorts = [];
  if (match) {
    var data = match[1];
    var sp = data.split('<br>')
    for (var i = 0; i < sp.length; i++) {
      var item = sp[i];
      //判断item是否是ip加端口
      var ip = item.match(/(\d+.+):(\d+)/)
      if (ip) {
        var id = match[1];
        var port = match[2];
        ipPorts.push(ip[1] + ':' + ip[2]);
      }
    }
    fetchBaidu(ipPorts);
  } else {
    console.log("未找到匹配的数据");
  }


  console.log('结束')

}

